/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.mail;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import po.Mail;
import po.User;
import service.MailService;
import service.UserService;

/**
 *
 * @author sony
 */
public class SendMailAction extends ActionSupport implements ServletRequestAware{

    private MailService mailService;
     private UserService userService;
    private HttpServletRequest request;
    private String title;
    private String account;
    private String content;
    private String toId;

    @Override
    public String execute() {
        if (ActionContext.getContext().getSession().get("user") == null) {
            return ERROR;
        } else {
            User usr = (User) ActionContext.getContext().getSession().get("user");
            String[] toIds = toId.split(";");
            for(int i=0;i<toIds.length;i++){
                int to = Integer.parseInt(toIds[i]);
                User toUser = userService.getUser(to);
                if(toUser==null)return ERROR;
                Mail mail = new Mail();
                mail.setUserByFromId(usr);
                mail.setUserByToId(userService.getUser(to));
                mail.setContent(content);
                mail.setIsread(0);
                mail.setTime(new Date());
                mail.setTitle(title);
                mailService.addMail(mail);
            }
        }
        request.setAttribute("url", "sendBox.action");
        return SUCCESS;
    }

    public MailService getMailService() {
        return mailService;
    }

    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }



    public void setServletRequest(HttpServletRequest arg0) {
        this.request=arg0;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getToId() {
        return toId;
    }

    public void setToId(String toId) {
        this.toId = toId;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


}

